package com.ldw.chapter8;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

@SpringBootApplication
public class Chapter8Application {

    public static void main(String[] args) {
        SpringApplication.run(Chapter8Application.class, args);
    }

@Bean
CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    //允许从百度站点跨域
    configuration.setAllowedOrigins(Arrays.asList("https://www.baidu.com"));
    //允许使用GET方法和POST方法
    configuration.setAllowedMethods(Arrays.asList("GET", "POST"));
    //允许带凭证
    configuration.setAllowCredentials(true);
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    //对所有URL生效
    source.registerCorsConfiguration("/**", configuration);
    return source;
}
}
